package com.aleaho.cftrack.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.aleaho.cftrack.R;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/12/4 22:54
 * @desc
 */
public class RadarView extends View {

    private Paint mBgPaint;
    private Paint mRadarPaint;

    private SweepGradient mRadarGradient;

    private Matrix mMatrix;

    private int mRadarRadius;

    private int mRadarStartColor = 0x0000ff00;
    private int mRadarEndColor = 0xaa00ff00;
    private int mRadarBackGroundColor = Color.WHITE;
    private int mRadarCircleColor = Color.GREEN;
    private int mRadarCircleNum = 5;

    private CharSequence mSearchText;

    private int mRotate = 0;

    private int MSG_WHAT = 1;
    private int DELAY_TIME = 50;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            mRotate +=3;
            postInvalidate();

            mMatrix.reset();
            mMatrix.preRotate(mRotate);
            mHandler.sendEmptyMessageDelayed(MSG_WHAT,DELAY_TIME);


        }
    };


    public RadarView(Context context) {
        this(context,null);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);


        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setColor(mRadarBackGroundColor);
        mBgPaint.setStyle(Paint.Style.FILL);

        mRadarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRadarPaint.setColor(mRadarCircleColor);
        mRadarPaint.setStyle(Paint.Style.STROKE);
        mRadarPaint.setStrokeWidth(2);

        mRadarGradient = new SweepGradient(0,0,mRadarStartColor,mRadarEndColor);

        mMatrix = new Matrix();
    }

    public void init(Context context, AttributeSet attrs){
        if (attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RadarView);
            mRadarBackGroundColor = ta.getColor(R.styleable.RadarView_mRadarBackGroundColor,mRadarBackGroundColor);
            mRadarStartColor = ta.getColor(R.styleable.RadarView_mRadarStartColor,mRadarStartColor);
            mRadarEndColor = ta.getColor(R.styleable.RadarView_mRadarEndColor,mRadarEndColor);
            mRadarCircleColor = ta.getColor(R.styleable.RadarView_mRadarCircleColor,mRadarCircleColor);
            mRadarCircleNum = ta.getInteger(R.styleable.RadarView_mRadarCircleNum,mRadarCircleNum);
            ta.recycle();
        }
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRadarRadius = Math.min(w/2,h/2);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mBgPaint.setShader(null);

        //移动至坐标圆心
        canvas.translate(mRadarRadius,mRadarRadius);

        //画大圈
        canvas.drawCircle(0,0,mRadarRadius,mBgPaint);

        //画小圈
        for (int i = 1; i <=mRadarCircleNum ; i++) {
            canvas.drawCircle(0,0,mRadarRadius * i/mRadarCircleNum,mRadarPaint);
        }

        //画线X方向
        canvas.drawLine(-mRadarRadius,0,mRadarRadius,0,mRadarPaint);

        //画线Y方向
        canvas.drawLine(0,-mRadarRadius,0,mRadarRadius,mRadarPaint);

        //设置扫描渐变
        mBgPaint.setShader(mRadarGradient);

        //设置矩阵
        canvas.concat(mMatrix);
        canvas.drawCircle(0,0,mRadarRadius,mBgPaint);



    }

    public void startRadar(){
        mHandler.removeMessages(MSG_WHAT);
        mHandler.sendEmptyMessage(MSG_WHAT);
    }

    public void stopRadar(){
        mHandler.removeMessages(MSG_WHAT);
    }
}
